package struct_demo

import (
	"fmt"
	"reflect"
)

type People struct {
	Name    string
	Age     int
	Address string
	Gender  string
}

func (p *People) ToString() string {
	return fmt.Sprintf("ToString: [Name: %s, Age: %d, Address: %s, Gender: %s]", p.Name, p.Age, p.Address, p.Gender)
}

func (p *People) Eat(food string) {
	fmt.Println(fmt.Sprintf("%s吃%s\n", p.Name, food))
}

func (p *People) changeName() {
	p.Name = "智棉科技"
}

func CreateTest() {
	var p1 People
	p1.Name = "aa"
	p1.Age = 18
	p1.Address = "12"
	fmt.Println(p1)

	p2 := People{
		Name:    "jack",
		Age:     22,
		Address: "usa",
		Gender:  "male",
	}
	fmt.Println(p2)
	fmt.Println(reflect.TypeOf(p2))

	var p3 People = People{"acy", 11, "北京", "male"}
	fmt.Println(p3)
	p3.Age = 30
	fmt.Println(p3, p3.Age)

	var p4 = p3
	//true
	fmt.Println(p4 == p3)
	p4.Name = "cottech"
	fmt.Println(p3, p4)
	//false
	fmt.Println(p3 == p4)

	var p5p *People = &p3
	p5p.Name = "lenovo"
	fmt.Println(*p5p, p3)

	var p6p = new(People)
	p6p.Name = "aa"
	fmt.Println(*p6p, reflect.TypeOf(p6p))

	fmt.Println(p4.ToString())
	fmt.Println(p6p.ToString())

	p4.Eat("盖饭")

	var p5 = People{"acy", 11, "北京", "male"}
	fmt.Println(p5)
	p5.changeName()
	fmt.Println(p5)
}

type Person struct {
	Name   string
	Age    int
	Mobile Phone
}

type Phone struct {
	Mode  string
	Price float32
}

func InnerDemo() {
	var p Person
	p.Name = "yu"
	p.Age = 10
	p.Mobile.Mode = "14pro"
	p.Mobile.Price = 1000
	fmt.Println(p)

	var xiaomi = Phone{"15 pro", 6000.00}
	var my = Person{"my", 30, xiaomi}
	fmt.Println(my)
}

type Mobile struct {
	Name  string
	Mode  string
	Price float32
}

type Cpu struct {
	Name  string
	Speed float32
}

type Info struct {
	Mobile
	Cpu
}

func CombDemo() {
	var i Info
	//使用组合其他结构体形成的结构体
	//赋值时候可以不携带前缀
	i.Mode = "15pro"
	i.Price = 1000
	i.Speed = 4.0
	//但是如果出现命名冲突，则需要使用前缀
	//i.Name = "1"
	i.Mobile.Name = "xiaomi"
	i.Cpu.Name = "骁龙8 Elit"
	fmt.Println(i)
	fmt.Printf("%s品牌%s型号手机使用的cpu型号是%s\n", i.Mobile.Name, i.Mode, i.Cpu.Name)
}
